# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=grpc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.29.1
_abseil_ver=20200225.2
pkgrel=1
pkgdesc="gRPC - Google's high performance, open source, general RPC framework (mingw-w64)"
arch=('any')
url='https://www.grpc.io/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf>=3.5.0"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "automake" "autoconf")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/google/grpc/archive/v${pkgver}.tar.gz"
        "abseil-cpp-${_abseil_ver}.tar.gz::https://github.com/abseil/abseil-cpp/archive/${_abseil_ver}.tar.gz"
        001-fix-linking.patch
        002-fix-verify-_WIN32_WINNT.patch)
sha256sums=('0343e6dbde66e9a31c691f2f61e98d79f3584e03a11511fad3f10e3667832a45'
            'f41868f7a938605c92936230081175d1eae87f6ea2c248f41077c8f88316f111'
            'c181c8a8d1b17b4f600369ca676efb54e5268542a9f9641bce3d5669e9b465d2'
            '36669a7296e4c168b0880f9bac93beeb919775c789d0675d1f5b9bd0886fb706')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-fix-linking.patch
  patch -p1 -i ${srcdir}/002-fix-verify-_WIN32_WINNT.patch

  cd ${srcdir}

  rmdir ${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp
  mv abseil-cpp-${_abseil_ver} ${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}

  # remove __USE_MINGW_ANSI_STDIO=1 define, it breaks grpc string handling for
  # cases like 'gpr_log(GPR_DEBUG, "Failed to free %" PRIuPTR ...'
  CPPFLAGS=${CPPFLAGS//-D__USE_MINGW_ANSI_STDIO=1/}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -Wno-dev \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DCMAKE_SKIP_RPATH="ON" \
      -DBUILD_SHARED_LIBS="ON" \
      -DgRPC_ZLIB_PROVIDER="package" \
      -DgRPC_CARES_PROVIDER="package" \
      -DgRPC_PROTOBUF_PROVIDER="package" \
      -DgRPC_SSL_PROVIDER="package" \
      -DgRPC_GFLAGS_PROVIDER="package" \
      -DgRPC_BENCHMARK_PROVIDER="package" \
      ../${_realname}-${pkgver}

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  #make test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
